package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.TodoDao;
import com.itheima.pojo.PictureSort;
import com.itheima.pojo.Todo;
import com.itheima.service.TodoService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.vo.TodoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class TodoServiceImpl implements TodoService {
    @Autowired
    private TodoDao todoDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 添加代办事项
     *
     * @param todoVO
     * @return
     */
    @Override
    public void add(TodoVO todoVO, String adminId) {
        Todo todo = new Todo();
        todo.setUid(idWorker.nextId() + "");
        todo.setAdminUid(adminId);
        todo.setText(todoVO.getText());
        todo.setDone(todoVO.getDone());
        todo.setStatus(1);
        todo.setCreateTime(DateUtils.getNowTime());
        todo.setUpdateTime(DateUtils.getNowTime());
        todoDao.insert(todo);
    }

    /**
     * 删除代办事项
     *
     * @param todoVO
     * @return
     */
    @Override
    public void delete(TodoVO todoVO) {
        //逻辑删除修改状态码
        Todo todo = new Todo();
        todo.setUid(todoVO.getUid());
        todo.setStatus(0);
        todoDao.updateByPrimaryKeySelective(todo);
    }

    /**
     * 编辑代办事项
     *
     * @param todoVO
     * @return
     */
    @Override
    public void edit(TodoVO todoVO) {
        Todo todo = new Todo();
        todo.setUid(todoVO.getUid());
        todo.setText(todoVO.getText());
        todo.setDone(todoVO.getDone());
        todo.setUpdateTime(DateUtils.getNowTime());
        todoDao.updateByPrimaryKeySelective(todo);
    }

    /**
     * 查询代办事项
     *
     * @param todoVO
     * @return
     */
    @Override
    public Page<Todo> getListPage(TodoVO todoVO) {
        //设置默认当前页
        if (todoVO.getCurrentPage() == null || "".equals(todoVO.getCurrentPage())) {
            todoVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (todoVO.getPageSize() == null || "".equals(todoVO.getPageSize())) {
            todoVO.setPageSize(10);
        }
        //当前页
        Integer currentPage = todoVO.getCurrentPage();
        //每页数据
        Integer pageSize = todoVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 1);
        return (Page<Todo>) todoDao.selectByExample(example);
    }
}
